<?php

/**
 * This is the model class for table "adminhtml_log".
 *
 * The followings are the available columns in table 'adminhtml_log':
 * @property string $log_id
 * @property string $log_time
 * @property integer $action_type
 * @property string $action_info
 * @property string $action_controller
 * @property string $action_action
 * @property string $action_params
 * @property string $action_model
 * @property integer $admin_id
 * @property string $remote_addr
 * @property integer $status
 *
 * The followings are the available model relations:
 * @property AdminhtmlAdmin $admin
 */
class AdminhtmlLog extends AdminhtmlCAR
{
	const ACTION_TYPE_LOGIN = 1;
	const ACTION_TYPE_CREATE= 2;
	const ACTION_TYPE_UPDATE= 3;
	const ACTION_TYPE_DELETE= 4;
	
	public function getResourceName()
	{
		return '管理员日志';
	}
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return AdminhtmlLog the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'adminhtml_log';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('action_type, action_info, action_controller, action_model', 'required'),
			array('action_type, admin_id, status', 'numerical', 'integerOnly'=>true),
			array('action_info', 'length', 'max'=>255),
			array('action_controller, action_model', 'length', 'max'=>50),
			array('remote_addr', 'length', 'max'=>20),
			array('log_time', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('log_id, log_time, action_type, action_info, action_controller, action_model, admin_id, remote_addr, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'admin' => array(self::BELONGS_TO, 'AdminhtmlAdmin', 'admin_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'log_id' => 'ID',
			'log_time' => '时间',
			'action_type' => '事件类型',
			'action_info' => '事件摘要',
			'action_controller' => '操作Controller',
			'action_model' => '操作Model',
			'admin_id' => '操作人',
			'remote_addr' => '操作IP',
			'status' => '状态',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('log_id',$this->log_id,true);
		$criteria->compare('log_time',$this->log_time,true);
		$criteria->compare('action_type',$this->action_type);
		$criteria->compare('action_info',$this->action_info,true);
		$criteria->compare('action_controller',$this->action_controller,true);
		$criteria->compare('action_model',$this->action_model,true);
		$criteria->compare('admin_id',$this->admin_id);
		$criteria->compare('remote_addr',$this->remote_addr,true);
		$criteria->compare('status',$this->status);
		
		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	public function getActionType()
	{
		$array= array(
			self::ACTION_TYPE_LOGIN  => '登录事件',
			self::ACTION_TYPE_CREATE => '创建数据',
			self::ACTION_TYPE_UPDATE => '更新数据',
			self::ACTION_TYPE_DELETE => '删除数据',
		);
		return $array;
	}
	/**
	 * Write action record into table.
	 * 
	 * @param integer $type
	 * @param array $option 
	 *    array(
	 *    	'action_info'=> 'action description',
	 *    	'action_model'=> 'resource name',
	 *    )
	 */
	public function actionRecord($type=self::ACTION_TYPE_LOGIN, $option=array() )
	{
		$adminId= Yii::app()->admin->getState('admin_id');
		if(AdminhtmlAdmin::model()->findByPk($adminId)) {
			$option['action_type'] = $type;
			$option['action_info'] = $option['action_info'];
			$option['action_model']= $option['action_model'];
			$option['action_controller']= Yii::app()->getController()->getRoute();
			$option['admin_id']	  = $adminId;
			$option['remote_addr']= ip2long(Yii::app()->request->getUserHostAddress() );
			$option['log_time']   = date('Y-m-d H:i:s');
			$this->attributes= $option;
			$this->save();
		}
	}
	
}